//index.js
const app = getApp()

Page({
  data: {
    dutyList: []
  },
  setTodayDutyList(dutyList) {
    this.setData({
      dutyList
    })
  },
  loadUserInfo() {
    return wx.cloud.callFunction({
      name: 'getUserInfo'
    }).then((res) => {
      app.globalData.setUserToken(res.result.userToken);
    }, (err) => {
      console.log('获取用户信息失败', err);
    });
  },
  loadTodayDuty() {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 1000
    })
    this.loadUserInfo().then(() => {
      let userToken = app.globalData.getUserToken();
      wx.cloud.callFunction({
        name: 'getUserTodayDuty',
        data: {
          userToken
        }
      }).then(res => {
        if (res && res.result && res.result.data instanceof Array) {
          this.setTodayDutyList(res.result.data);
        }
      }, err => {
        console.log('获取今日任务失败', err)
      })
    })
  },
  onLoad: function() {
    this.loadTodayDuty()
  },
  onShow: function() {
    this.loadTodayDuty();
  }

})
